<template>
	<view class="container">
		<view class="intro">
			<image src="/static/images/logo.png"></image>
			<view class="tips">
				一杯好茶，一口软欧包
				<br>
				在奈雪遇见两种美好
			</view>
		</view>
		<view class="bottom">
			<!-- #ifdef H5 -->
				<button type="primary" size="default" class="login-btn">
					登录
				</button>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
				<button type="primary" size="default" class="login-btn" open-type="getUserInfo" lang="zh_CN"
				 @getuserinfo="getUserInfo" @click="login()">
					<image src="/static/images/mine/wechat.png"></image>
					微信一键登录
				</button>
			<!-- #endif -->
			<view class="d-flex flex-column justify-content-evenly align-items-center text-center" style="height: 30vh;">
				<view class="w-100 font-size-base text-color-assist">新用户登录即加入会员，享会员权益</view>
				<view class="w-100 row d-flex just-content-around align-items-center font-size-sm text-color-assist">
					<view class="grid">
						<image src="/static/images/mine/rhyl.png"></image>
						<view>入会有礼</view>
					</view>
					<view class="grid">
						<image src="/static/images/mine/jfdh.png"></image>
						<view>积分兑换</view>
					</view>
					<view class="grid">
						<image src="/static/images/mine/sjtq.png"></image>
						<view>升级特权</view>
					</view>
					<view class="grid">
						<image src="/static/images/mine/srtq.png"></image>
						<view>生日特权</view>
					</view>
					<view class="grid">
						<image src="/static/images/mine/nxbz.png"></image>
						<view>奈雪宝藏</view>
					</view>
				</view>
				<view class="font-size-base text-color-primary">会员权益说明</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Member from '@/api/member'
	import {mapMutations} from 'vuex'
	import User  from '../../main.js'
    
	export default {
		data() {
			return{
				mtuser:{
					userId:"",
					userName:"",
					user_avatarUrl:"",
					userSex:0	
				    }
			      
				  }
			openId:""
			nickName:""
		
		},
		created() {
			this.getUserInfo()
		 }
     ,
		methods: {
			async getUserInfo(){
			 let _this = this ;
				uni.getProvider({
					// oauth  代表授权登录
				    service: 'oauth',
				    success: function (res) {
						// 登录
				        uni.login({
						   // 表示授权方式  如果不设置则弹出登录列表选择界面
				           provider: 'weixin',
				           success: function (res) {
							    // 微信公共平台获取
								// APPID
							   	let appid = 'wx2126f1ab3cb9d47d'
							   	// AppSecret
								let secret = '3c6fdecfe071c3bcbbe34b2e375647e9'
								
								// APPID
							// 	 let appid = 'wx226efd70a4d5c1ab'
							// 	  	// AppSecret
							// let secret = '36558a7ad403539da5553ac9a3d0f690'
							// let appid = 'wx1fde4b84f2b4ee7d'
							// 	  	// AppSecret
							// let secret = '479cc0824ff53014e7a99daebb5c1dda'
							   	let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' +
							   	res.code + '&grant_type=authorization_code';
							   	uni.request({
							   		url: url, // 请求路径
							   		success: result =>{
									console.log("OPENID")
										// 获取Id
							   	  console.log(result.data.openid);
								  _this.openId = result.data.openid  
								    _this.mtuser.userId = result.data.openid 
								     User.userId = _this.mtuser.userId
								  
							   		},
							   	});							   			
							   // 获取用户信息
				               uni.getUserInfo({
				               	provider: 'weixin',
				               	success: function(infoRes) {
				               		console.log('-------获取微信用户所有-----')
									 console.log(infoRes)
				               		// console.log(JSON.stringify(infoRes.userInfo).nickName);	
							    _this.mtuser.userName = (infoRes.userInfo).nickName	
							    _this.mtuser.user_avatarUrl=(infoRes.userInfo).avatarUrl;													                        _this.mtuser.userSex=infoRes.userInfo.gender
								  console.log("******"+ _this.mtuser.userSex)
				               	    }
				                });
				               }
				           });
				        }
				    })
				
				
			},
			login(){
				 console.log(this.mtuser);	
				 this.$http("Login/firstlogin/"+this.openId).then(
				 	 (res)=>{
						console.log(res)
							if(res[1].data.code==200){
								this.Userdata()
								User.userId=this.openId
								// User.logionstate = 2
								uni.reLaunch({							       
								       url: '/pages/index/index'
									   
								            });	
							}else{
								//数据库没有该用户的话，我们就给他添加一个新的用户
							 	this.AddUser()
								this.NewSign()
								this.NewMoney()
								// //注册后我们添加到数据库中
							}
					}
				 )
			}
			,
			//用户的数据
			Userdata(){
				this.$http("Login/login/"+this.openId).then(
				  (res)=>{
					   console.log(res)
					   console.log(res[1].data.data.userName)
					   console.log(res[1].data.data.userId)
				  }
				 )
			}
		   ,
		    //添加用户方法
		    AddUser(){
			
			uni.request({
			url:"http://127.0.0.1:8089/Login/addUser",
			method:"POST",
			data:this.mtuser,
			success: (res) => {
			console.log(res)
			 }
		})
		   }
		  ,
		  //新用户积分
		  NewSign(){
			this.$http("Sign/newsign/"+this.openId).then(
			 (res)=>{
			 console.log(res)
			 }
			)
		},
		//新用户余额
		NewMoney(){
			this.$http("Money/newmoney/"+this.openId).then(
			 (res)=>{
				 console.log(res)
			 }
			)
		}
		}
	}
</script>

<style lang="scss" scoped>
	.intro {
		width: 100%;
		height: 60vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-evenly;
		font-size: $font-size-base;
		color: $text-color-assist;
		
		image {
			width: 165rpx;
			height: 165rpx;
		}
		
		.tips {
			line-height: 72rpx;
			text-align: center;
		}
	}
	
	.bottom {
		height: 40vh;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 0 40rpx;
		
		.login-btn {
			width: 100%;
			border-radius: 50rem !important;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 10rpx 0;
			
			image {
				width: 36rpx;
				height: 30rpx;
				margin-right: 10rpx;
			}
		}
		
		.row {
			.grid {
				width: 20%;
				image {
					width: 60rpx;
					height: 60rpx;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>
